using AntDesign;
using AntDesign.TableModels;
using Microsoft.AspNetCore.Components.Web;
using SE.PO.CInterface.DBProtocols.SUs;

namespace SE.PO.CInterface.Web.Pages.DB.SU
{
    public partial class SUTable
    {
        private AddSUFrom _addFrom = default!;
        private EditSUFrom _editFrom = default!;
        private DelSUFrom _delFrom = default!;
       
        #region query

        List<SUDto> _source = new();

        IEnumerable<SUDto>? _selectedRows;

        ITable? _table;

        int _pageIndex = 1;
        int _pageSize = 10;
        int _total;

        protected override async Task OnInitializedAsync()
        {
            await GetSourceAsync(_pageIndex, _pageSize);
        }

        public async Task OnChange(QueryModel<SUDto> queryModel)
        {
            await GetSourceAsync(_pageIndex, _pageSize);
        }

        public async Task GetSourceAsync(int pageIndex, int pageSize)
        {
            var skipCount = (pageIndex - 1) * pageSize;
            var result = await Client.GetPage("", skipCount, pageSize);
            if (result != null)
            {
                _source = result.Items.ToList();
                _total = _source.Count;
            }
        }

        #endregion

        #region add

        bool _addVisible;

        private void AddHandleOk(MouseEventArgs e)
        {
            _addFrom.Form.Submit();
        }

        private void AddHandleCancel(MouseEventArgs e)
        {
            Console.WriteLine(e);
            _addVisible = false;
        }

        #endregion

        #region edit

        private CreateUpdateSUDto? _editModel;
        bool _editVisible;

        private void EditHandleOk(MouseEventArgs e)
        {
            _editFrom.Form?.Submit();
        }

        private void EditHandleCancel(MouseEventArgs e)
        {
            Console.WriteLine(e);
            _editVisible = false;
        }

        private void Edit(string id)
        {
            var dto = _source.FirstOrDefault(r => r.Id == id);
            if (dto != null)
            {
                _editModel = Mapper.Map<CreateUpdateSUDto>(dto);
            }
        }

        #endregion

        #region delete

        bool _delVisible;
        private SUDto? DelModel { get; set; } = new();


        private List<SUDto>? DelModels { get; set; } = new();

        private void DeleteHandleOk(MouseEventArgs e)
        {
            _delFrom.Form?.Submit();
        }

        private void DeleteHandleCancel(MouseEventArgs e)
        {
            Console.WriteLine(e);
            _delVisible = false;
        }

        private void DeleteSelectedRows()
        {
            DelModels = _selectedRows?.ToList();
            if (DelModels != null && DelModels.Any())
            {
                DelModel = DelModels.FirstOrDefault();
                _delVisible = true;
            }
        }

        private void Delete(string id)
        {
            DelModel = _source.FirstOrDefault(r => r.Id == id);
        }

        #endregion
    }
}